#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include "sdkconfig.h"

#define SDA_IO 18
#define SCL_IO 19
#define DEVICE_ADDRESS 0x38
#define WRITE_REGISTER_ADDRESS 0x70
//

void measure(){
    i2c_cmd_handle_t  cmd=i2c_cmd_link_create();
    i2c_master_start(cmd);

    i2c_master_write_byte(cmd,DEVICE_ADDRESS<<1,true);
    i2c_master_write_byte(cmd,WRITE_REGISTER_ADDRESS,true);
    i2c_master_write_byte(cmd,0xAC,true);
    i2c_master_write_byte(cmd,0x33,true);
    i2c_master_write_byte(cmd,0x00,true);

    i2c_master_stop(cmd);
    i2c_master_cmd_begin(0, cmd, 80 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);

}

void read_lux_task(void *args)
{
//    uint8_t *data_read=( uint8_t *)malloc(6);
    uint8_t readbuffer[6];

    while (true)
    {
        measure();

        i2c_cmd_handle_t  cmd=i2c_cmd_link_create();
        i2c_master_start(cmd);


        i2c_master_write_byte(cmd,(DEVICE_ADDRESS << 1) | I2C_MASTER_READ,true);
        //i2c_master_read(cmd,&data_read,6,I2C_MASTER_ACK);
        i2c_master_read_byte(cmd,readbuffer,0);
        i2c_master_read_byte(cmd, readbuffer+1, 0);
        i2c_master_read_byte(cmd, readbuffer+2, 0);
        i2c_master_read_byte(cmd, readbuffer+3, 0);
        i2c_master_read_byte(cmd, readbuffer+4, 0);
        i2c_master_read_byte(cmd, readbuffer+5, 1);
        i2c_master_stop(cmd);
        i2c_master_cmd_begin(0, cmd, 1000 / portTICK_RATE_MS);
        i2c_cmd_link_delete(cmd);

        uint16_t humity=(readbuffer[0]<<12|readbuffer[1]<<4)|readbuffer[2]>>4;
//        int  value=(((data_read[0]& 0x0F)<<16|data_read[4] << 8 | data_read[5])/100000)*50;
        printf("humity%d\n",humity*100/0x100000);

        uint16_t tempera=(readbuffer[3] & 0x0F) << 16 | readbuffer[4] << 8 | readbuffer[5];
        printf("tempera:%d\n",tempera*36/53473);

        vTaskDelay(1000 / portTICK_RATE_MS);


    }
}
void reset(){
    i2c_cmd_handle_t cmd=i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd,(DEVICE_ADDRESS<<1),true);
    i2c_master_write_byte(cmd,WRITE_REGISTER_ADDRESS,true);
    i2c_master_write_byte(cmd,0xBA,true);
    i2c_master_stop(cmd);
    i2c_master_cmd_begin(0,cmd,75/portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
}
void init(){
    i2c_cmd_handle_t cmd=i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd,(DEVICE_ADDRESS<<1),true);
    i2c_master_write_byte(cmd,WRITE_REGISTER_ADDRESS,true);
    i2c_master_write_byte(cmd,0xE1,true);
    i2c_master_write_byte(cmd,0x08,true);
    i2c_master_write_byte(cmd,0x00,true);
    i2c_master_stop(cmd);
    i2c_master_cmd_begin(0,cmd,75/portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
}
void app_main(void)
{
    i2c_config_t i2c_config = {
            .mode = I2C_MODE_MASTER,
            .sda_io_num = SDA_IO,
            .scl_io_num = SCL_IO,
            .sda_pullup_en = GPIO_PULLUP_ENABLE,
            .scl_pullup_en = GPIO_PULLUP_ENABLE,
            .master.clk_speed = 100000};

    i2c_param_config(I2C_NUM_0, &i2c_config);
    i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);

    reset();
    init();


    xTaskCreate(&read_lux_task, "lux", 8000, NULL, 1, NULL);



}
